package com.icarsaas.saas.master.web;

import com.icarsaas.saas.common.utils.ResultVoUtil;
import com.icarsaas.saas.common.vo.ResultVo;
import com.icarsaas.saas.component.actionLog.action.SaveAction;
import com.icarsaas.saas.component.actionLog.annotation.ActionLog;
import com.icarsaas.saas.component.actionLog.annotation.EntityParam;
import com.icarsaas.saas.component.shiro.ShiroUtil;
import com.icarsaas.saas.master.model.MasterTenant;
import com.icarsaas.saas.master.model.MasterTenantDetail;
import com.icarsaas.saas.master.service.MasterTenantDetailService;
import com.icarsaas.saas.master.service.MasterTenantService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author xrong120@gmail.com
 * @title: MasterTenantDetailContoller
 * @projectName mysaas
 * @description: 租户详情
 * @date 2022.03.26 16:08
 */
@Controller @Api(tags="租户详情模块")
@RequestMapping(value = "/masterTenantDetail/") @Slf4j
public class MasterTenantDetailContoller {

    @Autowired private MasterTenantService masterTenantService;
    @Autowired private MasterTenantDetailService masterTenantDetailService;

    @ModelAttribute
    public MasterTenantDetail get(@RequestParam(required = false) String id) {
        MasterTenantDetail entity = null;
        if (id != null) entity = masterTenantDetailService.get(id, null);
        else entity = new MasterTenantDetail();
        return entity;
    }

    /**
     * 跳转到细节页面
     */
    @GetMapping("masterTenantDetailForm")
    public String masterTenantDetail(@RequestParam(value = "masterTenantId", required = false) String masterTenantId, Model model) {
        if (StringUtils.isEmpty(masterTenantId)) {
            masterTenantId = ShiroUtil.getSubject().getTenant();// 获取当前登录用户的租户ID
        }
        MasterTenant masterTenant = masterTenantService.findByTenantId(masterTenantId);
        MasterTenantDetail entity = masterTenant.getMasterTenantDetail();
        if (entity == null) {//对于历史数据，在新增时如果没有初始化，则初始化
            entity = new MasterTenantDetail();
            entity.setMasterTenant(masterTenant);
            entity = masterTenantDetailService.save(entity);
            masterTenant.setMasterTenantDetail(entity);
            masterTenant = masterTenantService.save(masterTenant);
        }
        model.addAttribute("entity", entity);
        return "/masterTenant/masterTenantDetailForm";
    }

    /**
     * 保存添加/修改的数据
     */
    @PostMapping({"/saveEntity"}) @ResponseBody
    @ActionLog(name = "租户详情管理", message = "租户详情ID：${id}", action = SaveAction.class)
    public ResultVo<?> saveDetailEntity(@EntityParam MasterTenantDetail entity, String tenantName, String shortName) {
        masterTenantDetailService.save(entity);
        MasterTenant main = entity.getMasterTenant();
        main.setTenantName(tenantName);
        main.setShortName(shortName);
        masterTenantService.save(main);
        return ResultVoUtil.SAVE_SUCCESS;
    }
}
